package itsm.isperp.report.paser;

import itsm.isperp.framework.utils.DateUtils;
import itsm.isperp.module.entity.kpi.KpiSetting;
import itsm.isperp.report.data.warehouse.QueryResponse;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import me.chanjar.weixin.common.util.StringUtils;

/**
 * 
 * 事件请求解析器---解析器只是生产力查询的 from和where条件
 * 
 * @author lizx
 * @date 2015-8-24
 * @since 1.0
 * 
 */
public class QueryConditionPaser {

	public static class Comparer {

		String value;

		String column;

		int rate;

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public int getRate() {
			return rate;
		}

		public void setRate(int rate) {
			this.rate = rate;
		}

		public String getColumn() {
			return column;
		}

		public void setColumn(String column) {
			this.column = column;
		}

	}

	/**
	 * 
	 * 根据页面查询条件解析查询语句
	 * 
	 * @param isNot
	 * @param appendDateQuery
	 * @param setting
	 * @param req
	 * @return
	 * @throws ParseException
	 */
	public static QueryResponse paserQueryContitionFromRequest(boolean isNot,
			boolean appendDateQuery, KpiSetting setting, String dtype,
			HttpServletRequest req) throws ParseException {
		QueryResponse qr = new QueryResponse();
		StringBuilder sb = new StringBuilder();

		String dtr1 = req.getParameter("dtr1");// 日期
		String dtr2 = req.getParameter("dtr2");// 担当
		String dtr3 = req.getParameter("dtr3");// 工程师
		String dtr4 = req.getParameter("dtr4");// 系统
		String dtr6 = req.getParameter("dtr6");// 项目
		String dtr8 = req.getParameter("dtr8");//请求人

		String dscl1 = req.getParameter("dscl1");// 部门
		String dscl2 = req.getParameter("dscl2");// 供应商

		String sd, ed;
		Date stDate = null, eDate = null;
		if (dtr1.contains("~")) {

			String[] arr = dtr1.split("~");

			sd = arr[0];
			ed = arr[1];
			stDate = DateUtils.parseDate(sd);

			eDate = DateUtils.parseDate(ed);

		} else {
			String[] s = dtr1.split("-");
			Calendar cal = Calendar.getInstance();

			if (s.length == 1) {

				cal.set(Calendar.DAY_OF_MONTH, 1);
				stDate = cal.getTime();
				sd = DateUtils.formatDate(cal.getTime());

				cal.set(Calendar.DAY_OF_MONTH,
						cal.getActualMaximum(Calendar.DAY_OF_MONTH));

				ed = DateUtils.formatDate(cal.getTime());
				eDate = cal.getTime();
			} else if ("month".equals(dtype) || s.length == 2) {
				cal.set(Calendar.YEAR, Integer.parseInt(s[0].trim()));
				cal.set(Calendar.MONTH, Integer.parseInt(s[1].trim()) - 1);
				cal.set(Calendar.DAY_OF_MONTH, 1);
				stDate = cal.getTime();
				sd = DateUtils.formatDate(stDate);
				cal.set(Calendar.DAY_OF_MONTH,
						cal.getActualMaximum(Calendar.DAY_OF_MONTH));
				eDate = cal.getTime();
				ed = DateUtils.formatDate(eDate);

			} else {

				cal.set(Calendar.YEAR, Integer.parseInt(s[0].trim()));
				cal.set(Calendar.MONTH, Integer.parseInt(s[1].trim()) - 1);
				cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s[2].trim()));
				stDate = cal.getTime();
				eDate = stDate;
				sd = DateUtils.formatDate(stDate);
				ed = sd;
			}

		}
		String dateColumn = setting.getQueryDateColumn();

		if (appendDateQuery) {
			sb.append(DateQueryPaser.parse(dateColumn, sd, ed).toString());
		}

		String inOrNotin = "";
		if (isNot) {
			inOrNotin = " NOT  ";
		}
		// IS
		if (StringUtils.isNotEmpty(dtr2)) {

			sb.append("  AND confirmtimeman_id " + inOrNotin + " IN (");
			String ss = "";
			String[] sr = dtr2.split(",");
			for (String st : sr) {
				ss += ",'" + st + "'";
			}

			sb.append(ss.substring(1)).append(")");
		}

		// 二线
		if (StringUtils.isNotEmpty(dtr3)) {

			sb.append("  AND solver_id " + inOrNotin + " IN  (");
			String ss = "";
			String[] sr = dtr3.split(",");
			for (String st : sr) {
				ss += ",'" + st + "'";
			}

			sb.append(ss.substring(1)).append(")");

		}
		
		if (StringUtils.isNotEmpty(dtr8)) {

      sb.append("  AND cus_user_id " + inOrNotin + " IN  (");
      String ss = "";
      String[] sr = dtr8.split(",");
      for (String st : sr) {
        ss += ",'" + st + "'";
      }

      sb.append(ss.substring(1)).append(")");

    }

		// 系统
		if (StringUtils.isNotEmpty(dtr4)) {

			sb.append("  AND icd_category_name  " + inOrNotin + " IN (");
			String ss = "";
			String[] sr = dtr4.split(",");
			for (String st : sr) {
				ss += ",'" + st + "'";
			}

			sb.append(ss.substring(1)).append(")");

		}
		
		//项目
		if (StringUtils.isNotEmpty(dtr6)) {
			sb.append("  AND project_id='"+dtr6+"' ");
		}

		// 担当部门
		if (StringUtils.isNotEmpty(dscl1)) {

			sb.append(" AND supporter_name  "

					+ " IN (SELECT u.fullname FROM app_user u INNER JOIN app_department b ON u.app_department_id=b.id WHERE b.treenode_path LIKE '%"
					+ dscl1 + "%')");

		}

		if (StringUtils.isNotEmpty(dscl2)) {
			// 工程师

			sb.append("  AND solver_name  "

					+ " IN (SELECT u.fullname FROM app_user u INNER JOIN app_department b ON u.app_department_id=b.id WHERE b.treenode_path LIKE '%"
					+ dscl2 + "%')");

		}

		qr.setWhereSql(sb);
		qr.setStartDate(stDate);
		qr.setEndDate(eDate);
		return qr;
	}

}
